<?php
/**
 *
 * @author iegss
 *
 */
class mobileapi_rpc_cart  extends mobileapi_frontpage {

	/**
	 *
	 * @var base_rpc_service
	 */
	var $rpcService;

	/**
	 */
	function __construct($app)
	{
		parent::__construct($app);

		$this->app = app::get('b2c');
		$this->verify_member();

		$this->rpcService = kernel::single('base_rpc_service');

		$this->mCart = kernel::single('b2c_mdl_cart');
		$this->objMath = kernel::single("ectools_math");

		$this->_request = kernel::single('base_component_request');
		$this->_response = kernel::single('base_component_response');
	}

    public function batch_add_cart($params = array(), $rpcService) {
        $goodsObj = json_decode($params['goods']);
        if(!is_array($goodsObj)) return;
        $_REQUEST = null;
        foreach ($goodsObj as $key => $value) {
            $_REQUEST['product_id'] = $value->product_id;
            $_REQUEST['num'] = $value->num;
            if($value->buy_code) $_REQUEST['buy_code'] = $value->buy_code;
            if($value->btype) $_REQUEST['btype'] = $value->btype;

            $this->add($_REQUEST);
            $_REQUEST = null;
        }

        return '添加成功';
    }

    public function add($filter){

    	$product_id = intval($filter['product_id']);
    	$num = isset($filter['num']) && intval($filter['num'])?intval($filter['num']):1;
    	$buy_code = trim($filter['buy_code']);

    	$db = kernel::database();
    	$product_info = $db->selectrow("SELECT goods_id,product_id, store,freez FROM `sdb_b2c_products` where product_id = '$product_id' ");
    	/*
    	if(!$product_info){
    		kernel::single('base_rpc_service')->send_user_error('need_login', '产品ID错误!');
    	}
    	*/

    	$goods['goods_id'] = $product_info['goods_id'];
    	$goods['product_id'] = $product_id;
    	$goods['num'] = $num;

    	$product_info['freez']=empty($product_info['freez'])?0:$product_info['freez'];
        $buy_store=$product_info['store'] - $product_info['freez'];
    	//库存检测
    	if((int)$num > $buy_store){
    		kernel::single('base_rpc_service')->send_user_error('store_error', '产品库存不够本次购买');
    	}

    	if($buy_code)$goods['extends_params'] = array('buy_code'=>"$buy_code");

    	$post_data['response_json'] = 'true';
    	$post_data['0'] = 'goods';
    	$post_data['goods'] = $goods;
    	//$post_data['goods'] = $goods;
    	$post_data['btype'] = isset($filter['btype']) ?trim($filter['btype']):'';


    	$re=$this->api_add_cart('goods',$post_data);

    	if($re['code'] == 'ok'){
    		return $re['info'];
    	}

    	kernel::single('base_rpc_service')->send_user_error('cart_add_error', $re['info']);

    }


    public function batch_remove($params = array(), $rpcService)
    {
        $obj = json_decode($params['items']);
        $mCartObject = app::get('b2c')->model('cart_objects');

        if(!is_array($obj)) return;
        foreach ($obj as $value) {
            if($value->obj_type == 'coupon'){
                $flag = $mCartObject->remove_object('coupon', $value->obj_ident, $msg);
            }elseif($value->obj_type == 'all'){
                $flag = $mCartObject->remove_object('', null, $msg);
            }else{
                $flag = $mCartObject->remove_object($value->obj_type, $value->obj_ident, $msg);
            }
        }

        return '删除成功';
    }


    private function api_add_cart($type='goods',$data){
    	$arr_objects = array();
    	if ($objs = kernel::servicelist('b2c_cart_object_apps'))
    	{
    		foreach ($objs as $obj)
    		{
    			if ($obj->need_validate_store()){
    				$arr_objects[$obj->get_type()] = $obj;
    			}
    		}
    	}

    	$errorRequest = true;

    	if (method_exists($arr_objects[$type], 'get_data'))
    	{
    		if (!$aData = $arr_objects[$type]->get_data($data,$msg))
    		{
    			return array('code'=>'fail','info'=>$msg);
    		}
    	}

    	/**
    	 * 处理校验各自的数据是否可以加入购物车
    	 */
    	if (!$arr_objects[$type])
    	{
    		$msg = app::get('b2c')->_('商品类型错误！');
    		return array('code'=>'fail','info'=>$msg);
    	}

    	$aData = $data;
    	$obj_cart_object = kernel::single('b2c_cart_objects');
    	if (!$obj_cart_object->check_store($arr_objects[$type], $aData, $msg))
    	{
    		return array('code'=>'fail','info'=>$msg);
    	}

    	if($aData['btype'] == 'is_fastbuy'){
    		$obj_ident = $obj_cart_object->add_object($arr_objects[$type], $aData, $msg,true);
    	}else{
    		$obj_ident = $obj_cart_object->add_object($arr_objects[$type], $aData, $msg);
    	}

    	//echo md5("26");
    	//echo $obj_ident;exit;//goods_2_19

    	if(!$obj_ident){
    		return array('code'=>'fail','info'=>$msg);
    	}else{
    		return array('code'=>'ok','info'=>$obj_ident);
    	}

    }

    public function add_coupon($param){


    	switch ($param['isfastbuy']) {
    		case 'true':
    			$isfastbuy = true;
    			break;

    		case 'group':
    			$isfastbuy = 'group';
    			break;

    		default:
    			$isfastbuy = false;
    			break;
    	}
    	if(!empty($param['old_coupon'])){
			if($param['old_coupon']!=$param['coupon']){
				$ident = 'coupon_'.$param['old_coupon'];
				$mCartObject = app::get('b2c')->model('cart_objects');
				$msg='';
				$flag = $mCartObject->remove_object('coupon', $ident, $msg);
				if( !$flag ){
					kernel::single('base_rpc_service')->send_user_error('delect_coupon_error', $msg);
                                }elseif($param['coupon'] == "null"){
                                    $arr_json_data = array(
    					'success'=>app::get('b2c')->_('优惠券取消成功！'),
    					'md5_cart_info'=>kernel::single('b2c_cart_objects')->md5_cart_objects(),
                                    );
                                    return $arr_json_data;
                                }
			}
		}

    	  $aData = Array(
    	  		'coupon' => trim($param['coupon']),
    	  		'is_fastbuy' => $isfastbuy,
    	  		'response_json' => true,
    	  		'0' => 'coupon');

    	  	$this->api_add_cart('coupon',$aData);

    		$this->_common(1,$isfastbuy);


    		//只输出能够使用的优惠劵信息
    		$return_coupon_data = array();
    		foreach($this->pagedata['aCart']['object']['coupon'] as $key=>$coupon_data){
    			if($coupon_data['used'] == 'true'){
    				$return_coupon_data[$key]['coupon'] = $coupon_data['coupon'];
    				$return_coupon_data[$key]['cpns_id'] = $coupon_data['cpns_id'];
    				$return_coupon_data[$key]['name'] = $coupon_data['name'];
    				$return_coupon_data[$key]['obj_ident'] = $coupon_data['obj_ident'];
    			}
    		}
    		if($return_coupon_data){
    			$arr_json_data = array(
    					'success'=>app::get('b2c')->_('优惠券使用成功！'),
    					'coupon_info'=>$return_coupon_data,
    					'md5_cart_info'=>kernel::single('b2c_cart_objects')->md5_cart_objects(),
    			);
    			return $arr_json_data;
    		}else{
    			$ident = 'coupon_'.$aData['coupon'];
    			$this->app->model('cart_objects')->remove_object('coupon', $ident, $msg);
                        if($param['is_return']){
                            return false;
                        }else{
                            kernel::single('base_rpc_service')->send_user_error('coupon_error', '优惠券添加使用失败!');
                        }
    			//$this->splash('error',$url,'优惠券使用失败！',true);
    		}
    }

    public function get_list(){
    	$this->_common(1);
    	$this->pagedata['aCart']['subtotal_prefilter'] = $this->objMath->number_minus(array($this->pagedata['aCart']['subtotal'], $this->pagedata['aCart']['discount_amount_prefilter']));
    	$this->pagedata['aCart']['promotion_subtotal'] = $this->objMath->number_minus(array($this->pagedata['aCart']['subtotal'], $this->pagedata['aCart']['subtotal_discount']));

    	$cartObj = $this->pagedata['aCart'];

    	if($cartObj && isset($cartObj['object'])){
    		if($cartObj['object']['goods']){
    			foreach ($cartObj['object']['goods'] as $k=>$v){
    				foreach ($cartObj['object']['goods'][$k]['obj_items']['products'] as $k2 => $v2) {
    					$cartObj['object']['goods'][$k]['obj_items']['products'][$k2]['thumbnail_url'] = kernel::single('base_storager')->image_path($v2['thumbnail']);
    				}
    			}
    		}

    	}


    	return $cartObj;
    }




    public function get_list_group_by_tip(){
        $this->_common(1);
        $this->pagedata['aCart']['subtotal_prefilter'] = $this->objMath->number_minus(array($this->pagedata['aCart']['subtotal'], $this->pagedata['aCart']['discount_amount_prefilter']));
        $this->pagedata['aCart']['promotion_subtotal'] = $this->objMath->number_minus(array($this->pagedata['aCart']['subtotal'], $this->pagedata['aCart']['subtotal_discount']));
        $cartObj = $this->pagedata['aCart'];
        $goods_list_group_by_tip=app::get('b2c')->model('goods_tip')->getList('*','',0,-1,'ordernum asc');
        if($cartObj && isset($cartObj['object'])){
            if($cartObj['object']['goods']){
                $obj_base_storager=kernel::single('base_storager');

                foreach ($cartObj['object']['goods'] as $k=>$v){
                    foreach ($cartObj['object']['goods'][$k]['obj_items']['products'] as $k2 => $v2) {
                        $cartObj['object']['goods'][$k]['obj_items']['products'][$k2]['thumbnail_url'] = $obj_base_storager->image_path($v2['thumbnail']);
                    }
                }
                $tip_items=array();
                foreach($goods_list_group_by_tip as &$tip_item){

                    foreach($cartObj['object']['goods'] as $goods_item){

                        $tip_id=app::get('b2c')->model('goods')->getList('tip_id',array('goods_id'=>$goods_item['obj_items']['products'][0]['goods_id']));
                //之前$tip_item寻址，$cartObj['list_group_by_tip']里goods重复。
                        // if($tip_id[0]['tip_id']==$tip_item['tip_id']){
                            $tip_item['goods'][]=$goods_item;
                            //寻址，第二次foreach会将之前的覆盖掉
                            //寻址问题，$tip_item里的东西会重复的加到$tip_items里 qianjin 2016-5-4
                            $tip_items[0]=$tip_item;
                        // }
                    }
                }
                $cartObj['list_group_by_tip']=$tip_items;
            }

        }

        unset($cartObj['object']['goods']);
        return $cartObj;
    }



    public function update() {
    	//$aParams = $this->_request->get_params(true);

    	$mCartObject = app::get('b2c')->model('cart_objects');

    	$obj_type = $_REQUEST['obj_type'];
    	$obj_ident = $_REQUEST['obj_ident'];
    	$arr_object['quantity'] = (float)$_REQUEST['quantity'];

    	$_flag = $mCartObject->update_object($obj_type, $obj_ident, $arr_object);

    	if( !$_flag ) {
    		kernel::single('base_rpc_service')->send_user_error('cart_update_error', '购物车修改失败！');
    	}

    	return $_flag;

    }

    public function remove() {
    	$mCartObject = app::get('b2c')->model('cart_objects');

    	$obj_type = $_REQUEST['obj_type'];
    	$obj_ident = $_REQUEST['obj_ident'];

    	if($obj_type == 'coupon'){
    		$flag = $mCartObject->remove_object('coupon', $obj_ident, $msg);
    	}elseif($obj_type == 'all'){
    		$flag = $mCartObject->remove_object('', null, $msg);
    	}else{
    		$flag = $mCartObject->remove_object($obj_type, $obj_ident, $msg);
    	}

    	if( !$flag ){
    		kernel::single('base_rpc_service')->send_user_error('cart_remove_error', $msg);
    	}

    	return $flag;

    }

    public function checkout(){
    	switch ($_REQUEST['isfastbuy']) {
    		case 'true':
    			$isfastbuy = true;
    		break;

    		case 'group':
    			$isfastbuy = 'group';
    		break;

    		default:
    			$isfastbuy = false;
    		break;
    	}


    	if($isfastbuy != 'group' ){
    		$this->mCart->unset_data();
    	}

        //会员信息
        $arrMember = kernel::single('b2c_user_object')->get_members_data(array('members'=>'member_id,cur',));
        $arrMember = $arrMember['members'];
    	$this->_common(false,$isfastbuy);
    	$tip_id_arr=array();
        $tip_goods = array();
    	$cartObj = $this->pagedata['aCart'];
    	if($cartObj && isset($cartObj['object'])){
    		if($cartObj['object']['goods']){
    			foreach ($cartObj['object']['goods'] as $k=>$v){
                    //购物车内商品库存不足时，阻止下单结算。 2017/9/20 by 马国民
                    if($cartObj['object']['goods'][$k]['quantity']=='0'){
                        kernel::single('base_rpc_service')->send_user_error('store_error','购物车内有商品库存不足,请检查后再结算');
                    }

                    $productPromotion= kernel::single('b2c_ctl_site_product')->_get_goods_promotion($v['params']['goods_id'],array(),$arrMember['member_lv_id']);
                    $promotion['goods'] = array();
                    $promotion['order'] = array();
                    foreach ($productPromotion['goods'] as $pmt){
                            $promotion['goods'][] = $pmt;
                    }
                    if(count($productPromotion['order'])>0) foreach ($productPromotion['order'] as $pmt){
                            $promotion['order'][] = $pmt;
                    }

    				foreach ($cartObj['object']['goods'][$k]['obj_items']['products'] as $k2 => $v2) {

                        $tip_id_arr[] = $v2['tip']['tip_id'];
                        $cartObj['object']['goods'][$k]['obj_items']['products'][$k2]['thumbnail_url'] = kernel::single('base_storager')->image_path($v2['thumbnail']);
                        //在提交时判断购物车内秒杀物品数量 2017-8-9 马国民
                        if ($cartObj['object']['goods'][$k]['obj_items']['products'][$k2]['whyshopseckill_id']) {
                            $db = kernel::database();
                            $seckillgoods_info = $db->selectrow("SELECT * FROM `sdb_whyshopseckill_whyshopseckill_goods` WHERE seckill_goods_id = '{$cartObj['object']['goods'][$k]['obj_items']['products'][$k2]['goods_id']}' AND whyshopseckill_id='{$cartObj['object']['goods'][$k]['obj_items']['products'][$k2]['whyshopseckill_id']}' ");
                            if ($cartObj['object']['goods'][$k]['quantity']>$seckillgoods_info['seckill_limit']) {
                                kernel::single('base_rpc_service')->send_user_error('store_error', $cartObj['object']['goods'][$k]['obj_items']['products'][$k2]['name'].'超出秒杀限量');
                            }
                            $member_purchase=$db->selectRow("select sum(i.nums) as total from sdb_b2c_order_items i where i.goods_id=".$cartObj['object']['goods'][$k]['obj_items']['products'][$k2]['goods_id']." and i.whyshopseckill_id=".$cartObj['object']['goods'][$k]['obj_items']['products'][$k2]['whyshopseckill_id']." and order_id in(select order_id from sdb_b2c_orders where pay_status='1' and member_id=".$arrMember['member_id'].")");
                            //每个会员购买数量
                            if (!empty($member_purchase)) {
                                $purchase=$member_purchase['total'];
                                if ($purchase>=$cartObj['object']['goods'][$k]['quantity']) {
                                    kernel::single('base_rpc_service')->send_user_error('store_error',$cartObj['object']['goods'][$k]['obj_items']['products'][$k2]['name'].'限购'.$seckillgoods_info['seckill_limit'].'件');
                                }
                            }
                        }
                        //拆分当前商品
                        if($v2['tip']['tip_id'] == '3'){    // 此处不能修改，已写死
                            $cat_id = app::get("b2c")->model("goods_cat")->getRow("parent_id",array('cat_id'=>$v2['cat_id']));
                            if($v2['cat_id'] == '64' || $cat_id['parent_id'] == '64'){  // 此处不能修改，已写死
                                $tip_goods[$v2['tip']['tip_id']][64][] = array("price"=>$v2['price']['member_lv_price'],"q"=>$v['quantity']);
                                $tip_goods[$v2['tip']['tip_id']]['all'][] = array("price"=>$v2['price']['member_lv_price'],"q"=>$v['quantity']);
                            }else{
                                $tip_goods[$v2['tip']['tip_id']][0][] = array("price"=>$v2['price']['member_lv_price'],"q"=>$v['quantity']);
                                $tip_goods[$v2['tip']['tip_id']]['all'][] = array("price"=>$v2['price']['member_lv_price'],"q"=>$v['quantity']);
                            }
                        }else{
                            $tip_goods[$v2['tip']['tip_id']]['all'][] = array("price"=>$v2['price']['member_lv_price'],"q"=>$v['quantity']);

                        }
                    }
    			}
    		}

    	}
        $todaytime = strtotime(date("Y-m-d 00:00:00",time()));
        $today_order = app::get("b2c")->model("orders")->getList("*",array('createtime|than'=>$todaytime,'status|noequal'=>"dead",'member_id'=>$arrMember['member_id']),0,-1);
        foreach($today_order as $order){
            $inorder .=$order["order_id"].",";
        }
        $sql = "select g.*,i.nums from ".kernel::database()->prefix."b2c_order_items i left join ".kernel::database()->prefix."b2c_goods g ON g.goods_id = i.goods_id "
                . "where i.order_id in (".trim($inorder,",").")";
        $today_goods = kernel::database()->selectLimit($sql,-1,0);
        foreach($today_goods as $g){


            if($g['tip_id'] == '3'){    // 此处不能修改，已写死
                $cat_id = app::get("b2c")->model("goods_cat")->getRow("parent_id",array('cat_id'=>$g['cat_id']));
                if($g['cat_id'] == '64' || $cat_id['parent_id'] == '64'){   // 此处不能修改，已写死
                    $tip_goods[$g['tip_id']][64][] = array("price"=>$g['price'],"q"=>$g['nums']);
                    $tip_goods[$g['tip_id']]['all'][] = array("price"=>$g['price'],"q"=>$g['nums']);
                }else{
                    $tip_goods[$g['tip_id']][0][] = array("price"=>$g['price'],"q"=>$g['nums']);
                    $tip_goods[$g['tip_id']]['all'][] = array("price"=>$g['price'],"q"=>$g['nums']);
                }
            }else{
                $tip_goods[$g['tip_id']]['all'][] = array("price"=>$g['price'],"q"=>$g['nums']);

            }
        }
        // foreach($tip_goods as $k=>$val){
        //     if($k=='4' || $k=='5' ){
        //         if(count($val['all'])>1){
        //             $amount = 0;
        //             foreach($val['all'] as $k3=>$v3){
        //                 $amount1 += $v3['price']*$v3['q'];
        //             }
        //             if($amount1 >= 1000){
        //                 kernel::single('base_rpc_service')->send_user_error('cart_remove_error',"根据海关政策要求，每人每天一次购买多件海外商品不能超过1000元，请调整购物车。");// "根据海关政策，除非是单件不可分割商品，购买海外直邮商品每人每天不可超过1000元，请调整购物车");
        //             }
        //         }else{
        //             if($val['all'][0]['q']>1){
        //                 if($val['all'][0]['q'] * $val['all'][0]['price'] >= 1000){
        //                     kernel::single('base_rpc_service')->send_user_error('cart_remove_error',"根据海关政策要求，每人每天一次购买多件海外商品不能超过1000元，请调整购物车。");// "根据海关政策，除非是单件不可分割商品，购买海外直邮商品每人每天不可超过1000元，请调整购物车");
        //                 }
        //             }
        //         }
        //     }else{
        //         foreach($val as $key=>$value){
        //             $amount = 0;
        //             if($key == '64'){
        //                 foreach($value as $k3=>$v3){
        //                     $amount2 += $v3['price']*$v3['q'];
        //                 }
        //                 if($amount2>=100){
        //                     kernel::single('base_rpc_service')->send_user_error('cart_remove_error', "根据海关政策，每人每天化妆品超过100元将征收50%的行邮税，为您的利益着想，请调整购物车商品");
        //                 }
        //             }elseif($key == '0'){
        //                 foreach($value as $k3=>$v3){
        //                     $amount3 += $v3['price']*$v3['q'];
        //                 }
        //                 if($amount3>=500){
        //                     kernel::single('base_rpc_service')->send_user_error('cart_remove_error', "根据海关政策，普通商品（不含化妆品等品类）每人每天超过500元将征收10%的行邮税，为您的利益着想，请调整购物车");
        //                 }
        //             }
        //         }
        //     }
        // }
            if(!$promotion['order']){
                $promotion['order'] = array();
            }
            if(!$promotion['goods']){
                $promotion['goods'] = array();
            }
            $this->pagedata['promotion_new'] = $promotion;
		$tip_id_one=array_unique($tip_id_arr);
		$mdl_goods_tip=app::get('b2c')->model('goods_tip');
		$tip_info=array();
		foreach($tip_id_one as $tip_id){
			if($tip_id){
				$tip_info[]=$mdl_goods_tip->getRow('*',array('tip_id'=>$tip_id));
			}
		}
		$this->pagedata['tip']=$tip_info;

    	$this->pagedata['aCart'] = $cartObj;

        $this->pagedata['checkout'] = 1;
        $this->pagedata['md5_cart_info'] = kernel::single("b2c_cart_objects")->md5_cart_objects($isfastbuy);

        $this->pagedata['member_id'] = $arrMember['member_id'];
        /** 判断请求的参数是否是group **/
        $arr_request_params = $this->_request->get_params();
        //是否是团购订单
        //$arr_request_params[0] == 'group' ? $this->pagedata['is_group_orders'] = 'true' : $this->pagedata['is_group_orders'] = 'false';
        $isfastbuy == 'group' ? $this->pagedata['is_group_orders'] = 'true' : $this->pagedata['is_group_orders'] = 'false';
        $this->pagedata['app_id'] = $app_id;

        // 如果会员已登录，查询会员的信息
        $obj_member_addrs = $this->app->model('member_addrs');
        $obj_dltype = $this->app->model('dlytype'); //配送方式model
        $member_point = 0; //会员积分
        $str_def_currency = $arrMember['cur'] ? $arrMember['cur'] : ""; //会员设置的默认货币

        /*获取收货地址 start*/
        $def_addr = kernel::single('b2c_member_addrs')->get_default_addr($arrMember['member_id']);
        $this->pagedata['def_addr'] = $def_addr;
        $member_addr_list = $obj_member_addrs->getList('*',array('member_id'=>$arrMember['member_id']));
        $this->pagedata['member_addr_list'] = $member_addr_list;
        //邮编是否开启
        $this->pagedata['site_checkout_zipcode_required_open'] = $this->app->getConf('site.checkout.zipcode.required.open');
        /* 是否开启配送时间的限制 */
        $this->pagedata['site_checkout_receivermore_open'] = $this->app->getConf('site.checkout.receivermore.open');
        /*收货地址 end*/



//        if($def_addr){
//            //是否有默认的当前的配送方式
//            $area = explode(':',$def_addr['area']);
//            //$this->pagedata['dlytype_html'] = kernel::single('b2c_order_dlytype')->select_delivery_method($this,$area[2],$this->pagedata['aCart']);
//            $this->pagedata['shipping_method'] = (isset($_COOKIE['purchase']['shipping']) && $_COOKIE['purchase']['shipping']) ? unserialize($_COOKIE['purchase']['shipping']) : '';
//            $this->pagedata['has_cod'] = $this->pagedata['shipping_method']['has_cod'];
//        }
	//增加对图片url的处理,2016-6-8 by jeff,因为delivery_change会导致图片url丢失
	$___arr_url = array();
	foreach ($this->pagedata['aCart']['object']['goods'] as & $___good) {
		$___pros = $___good['obj_items']['products'];
		foreach($___pros as & $___pro) {
			$___arr_url[$___pro['bn']] = $___pro['thumbnail_url'];
		}
	}
        $this->pagedata['shipping_method'] = $this->delivery_change();
	//还原thumbnail_url 2016-6-8 by jeff
	foreach ($this->pagedata['aCart']['object']['goods'] as & $___good) {
                $___pros = & $___good['obj_items']['products'];
                foreach($___pros as & $___pro) {
                        //$___arr_url[$___pro['bn']] = $___pro['thumbnail_url'];
                        $___pro['thumbnail_url']=$___arr_url[$___pro['bn']];
                }
        }


        $currency = app::get('ectools')->model('currency');
        if($this->pagedata['shipping_method']){
            // 是否有默认的支付方式
            $this->pagedata['arr_def_payment'] = (isset($_COOKIE['purchase']['payment']) && $_COOKIE['purchase']['payment']) ? unserialize($_COOKIE['purchase']['payment']) : '';
            /*支付方式列表*/
            $currency = app::get('ectools')->model('currency');

            $this->pagedata['currencys'] = $currency->getList('cur_id,cur_code,cur_name');
            if (!$str_def_currency){
                $arrDefCurrency = $currency->getDefault();
                $str_def_currency = $arrDefCurrency['cur_code'];
            }else{
                $arrDefCurrency = $currency->getcur($str_def_currency);
            }

            $aCur = $currency->getcur($str_def_currency);
            $this->pagedata['current_currency'] = $str_def_currency;
            $obj_payments = kernel::single('ectools_payment_select');

            $this->pagedata['payment_html'] = $obj_payments->select_pay_method($this, $arrDefCurrency, false,false,array('iscommon','iswap'),'API');
            //$obj_payment_select->select_pay_method($this, $sdf, false,false,array('iscommon','iswap'),'API');
            /*end*/

            $ret = $currency->getFormat();
            $ret =array(
                'decimals'=>$this->app->getConf('system.money.decimals'),
                'dec_point'=>$this->app->getConf('system.money.dec_point'),
                'thousands_sep'=>$this->app->getConf('system.money.thousands_sep'),
                'fonttend_decimal_type'=>$this->app->getConf('system.money.operation.carryset'),
                'fonttend_decimal_remain'=>$this->app->getConf('system.money.decimals'),
                'sign' => $ret['sign']
            );
            $this->pagedata['money_format'] = json_encode($ret);
        }

        //是否开启发票
        $trigger_tax = $this->app->getConf('site.trigger_tax');
        if($trigger_tax == 'true'){
            $personal_tax_ratio = $this->app->getConf('site.personal_tax_ratio'); //个人发票税率
            $company_tax_ratio = $this->app->getConf('site.company_tax_ratio'); //公司发票税率
            $tax_content = $this->app->getConf('site.tax_content'); //发票内容选项
            if($tax_content){
                $arr_tax_content = explode(',',$tax_content);
                foreach($arr_tax_content as $tax_content_value){
                    $select_tax_content[$tax_content_value] = $tax_content_value;
                }
            }
            $tax_setting = array(
                'trigger_tax' => $trigger_tax,
                'personal_tax_ratio' => $personal_tax_ratio,
                'company_tax_ratio' => $company_tax_ratio,
                'tax_content' =>$select_tax_content ? $select_tax_content : 0,
            );
            $this->pagedata['tax_setting'] = $tax_setting;
        }//end 发票

        /**
         取到优惠券的信息
         */
        $oCoupon = kernel::single('b2c_coupon_mem');
        $aData = $oCoupon->get_list_m($arrMember['member_id']);
        if( is_array($aData) ) {
            foreach( $aData as $_key => $_val ) {
                if( $_val['memc_used_times'] ) unset($aData[$_key]);
            }
        }
        $this->pagedata['coupon_lists'] = $aData;
        /*end*/


        $total_item = $this->objMath->number_minus(array($this->pagedata['aCart']["subtotal"], $this->pagedata['aCart']['discount_amount_prefilter']));
        // 取到商店积分规则
        $policy_method = $this->app->getConf("site.get_policy.method");
        switch ($policy_method)
        {
            case '1':
                $subtotal_consume_score = 0;
                $subtotal_gain_score = 0;
                $totalScore = 0;
                break;
            case '2':
                $subtotal_consume_score = round($this->pagedata['aCart']['subtotal_consume_score']);
                $policy_rate = $this->app->getConf('site.get_rate.method');
                $subtotal_gain_score = round($this->objMath->number_plus(array(0, $this->pagedata['aCart']['subtotal_gain_score'])));
                $totalScore = round($this->objMath->number_minus(array($subtotal_gain_score, $subtotal_consume_score)));
                break;
            case '3':
                $subtotal_consume_score = round($this->pagedata['aCart']['subtotal_consume_score']);
                $subtotal_gain_score = round($this->pagedata['aCart']['subtotal_gain_score']);
                $totalScore = round($this->objMath->number_minus(array($subtotal_gain_score, $subtotal_consume_score)));
                break;
            default:
                $subtotal_consume_score = 0;
                $subtotal_gain_score = 0;
                $totalScore = 0;
                break;
        }

        $total_amount = $this->objMath->number_minus(array($this->pagedata['aCart']["subtotal"], $this->pagedata['aCart']['discount_amount']));
        if ($total_amount < 0) $total_amount = 0;
        //获取默认身份证
        $this->pagedata['id_card'] = app::get('b2c')->model('addr_card')->getRow("*",array("member_id"=>$arrMember['member_id']));
        // 是否可以用积分抵扣
        $obj_point_dis = kernel::service('b2c_cart_point_discount');

        if ($obj_point_dis){
            $obj_point_dis->set_order_total($total_amount);
            $this->pagedata['point_dis_html'] = $obj_point_dis->get_html($arrMember['member_id']);
            //$this->pagedata['point_dis_js'] = $obj_point_dis->get_javascript($arrMember['member_id']);
        }

        // 得到cart total支付的信息
        $this->pagedata['order_detail'] = array(
            'cost_item' => $total_item,
            'total_amount' => $total_amount,
            'currency' => $this->app->getConf('site.currency.defalt_currency'),
            'pmt_order' => $this->pagedata['aCart']['discount_amount_order'],
            'pmt_amount' => $this->pagedata['aCart']['discount_amount'],
            'totalConsumeScore' => $subtotal_consume_score,
            'totalGainScore' => $subtotal_gain_score,
            'totalScore' => $totalScore,
            'cur_code' => $strDefCurrency,
            'cur_display' => $strDefCurrency,
            'cur_rate' => $aCur['cur_rate'],
            'final_amount' => $currency->changer($total_amount, $this->app->getConf("site.currency.defalt_currency"), true),
        );

        //会员积分
       $this->pagedata['order_detail']['totalScore'] = $member_point;
        $odr_decimals = $this->app->getConf('system.money.decimals');
        $total_amount = $this->objMath->get($this->pagedata['order_detail']['total_amount'], $odr_decimals);
        $this->pagedata['order_detail']['discount'] = $this->objMath->number_minus(array($this->pagedata['order_detail']['total_amount'], $total_amount));
        $this->pagedata['order_detail']['total_amount'] = $total_amount;
        $this->pagedata['order_detail']['current_currency'] = $strDefCurrency;

        // 获得商品的赠品信息
        $arrM_info = array();

        foreach ($this->pagedata['aCart']['object']['goods'] as $arrGoodsInfo){
            if (isset($arrGoodsInfo['gifts']) && $arrGoodsInfo['gifts']){
                $this->pagedata['order_detail']['gift_p'][] = array(
                    'storage' => $arrGoodsInfo['gifts']['storage'],
                    'name' => $arrGoodsInfo['gifts']['name'],
                    'nums' => $arrGoodsInfo['gifts']['nums'],
                );
            }

            // 得到商品购物信息的必填项目
            $goods_id = $arrGoodsInfo['obj_items']['products'][0]['goods_id'];
            $product_id = $arrGoodsInfo['obj_items']['products'][0]['product_id'];
            // 得到商品goods表的信息
            $objGoods = $this->app->model('goods');
            $arrGoods = $objGoods->dump($goods_id, 'type_id');
            if (isset($arrGoods) && $arrGoods && $arrGoods['type']['type_id']){
                $objGoods_type = $this->app->model('goods_type');
                $arrGoods_type = $objGoods_type->dump($arrGoods['type']['type_id'], '*');

                if ($_COOKIE['checkout_b2c_goods_buy_info']){
                    $goods_need_info = json_decode($_COOKIE['checkout_b2c_goods_buy_info'], 1);

                }
                if ($arrGoods_type['minfo']){
                    if ($arrGoodsInfo['obj_items']['products'][0]['spec_info']){
                        $arrM_info[$product_id]['name'] = $arrGoodsInfo['obj_items']['products'][0]['name'] . '(' . $arrGoodsInfo['obj_items']['products'][0]['spec_info'] . ')';
                    }else{
                        $arrM_info[$product_id]['name'] = $arrGoodsInfo['obj_items']['products'][0]['name'];
                    }
                    $arrM_info[$product_id]['nums'] = $this->objMath->number_multiple(array($arrGoodsInfo['obj_items']['products'][0]['quantity'],$arrGoodsInfo['quantity']));

                    foreach ($arrGoods_type['minfo'] as $key=>$arr_minfo){
                        if (isset($goods_need_info[$product_id][$key]) && $arr_minfo['label'] == $goods_need_info[$product_id][$key]['name']){
                            $arr_minfo['value'] = $goods_need_info[$product_id][$key]['val'][0];
                        }else{
                            $no_value = true;
                        }
                        $arrM_info[$product_id]['minfo'][] = $arr_minfo;
                    }
                }
            }
        }

        return $this->pagedata;


    }

    //配送方式根据送货地址联动
    public function delivery_change(){
    	//$this->set_header();

    	switch ($_REQUEST['isfastbuy']) {
    		case 'true':
    			$isfastbuy = true;
    		break;

    		case 'group':
    			$isfastbuy = 'group';
    		break;

    		default:
    			$isfastbuy = false;
    		break;
    	}

    	$this->_common(false,$isfastbuy);

    	$area_id = $_POST['area'];
    	$shipping_method = $_POST['shipping_method'];

    	$obj_delivery = new b2c_order_dlytype();
    	$sdf = array();
    	$sdf = $this->pagedata['aCart'];
    	if (isset($_POST['payment']) && $_POST['payment'])
    		$sdf['pay_app_id'] = $_POST['payment'];
    	$this->pagedata['app_id'] = $app_id;
    	$deliverys = $obj_delivery->select_delivery_method($this,$area_id,$sdf,$shipping_method,'API');

    	foreach ($deliverys as $key =>$v){
    		$deliverys[$key]['shipping'] = '{"id":'.$v['dt_id'].',"has_cod":"'.$v['has_cod'].'","dt_name":"'.$v['dt_name'].'","money":"'.$v['money'].'"}';

    	}
    	return $deliverys;
    }

    //支付方式根据配送方式联动
    public function payment_change(){

    	$obj_payment_select = new ectools_payment_select();
    	if($_POST['payment']['currency']){
    		$sdf['cur'] = $_POST['payment']['currency'];
    	}

    	if($_POST['shipping']){
    		$shipping = json_decode($_POST['shipping'],true);
    		$this->pagedata['has_cod'] = $shipping['has_cod'];
    	}
    	$currency = app::get('ectools')->model('currency');
    	$this->pagedata['currencys'] = $currency->getList('cur_id,cur_code,cur_name');
    	$this->pagedata['current_currency'] = $sdf['cur'] ? $sdf['cur'] : '';
    	$this->pagedata['app_id'] = $app_id;
    	$payments = $obj_payment_select->select_pay_method($this, $sdf, false,false,array('iscommon','iswap'),'API');
        //判断app版本
        $appVersion = $_POST['appVersion'];
        if (!$appVersion) {
            kernel::single('base_rpc_service')->send_user_error('appVersion', '此版本已停用，请下载最新版本');
            return false;
        }
    	$payments_ret = array();
    	foreach ($payments as $key => $value) {
            if ($value['app_pay_brief']==null) {
                $value['app_pay_brief'] = '';
            }
             if ($value['pay_fee']==null) {
                $value['pay_fee'] = '';
            }
             if ($value['is_cod']==null) {
                $value['is_cod'] = '';
            }
             if ($value['app_pay_type']==null) {
                $value['app_pay_type'] = '';
            }
            if ($value['support_cur']==null) {
                $value['support_cur'] = '';
            }
    	    //2016-5-31 by jeff,过滤掉微信公众号支付
            //对接通联支付时，"app_order_by"字段的值 "1"，正确是整型，系统是字符串（错误）。导致APP，支付列表显示失败
    	    if($value['app_class']=='weixin_payment_plugin_wxpayjsapi' || $value['app_class']=='weixin_payment_plugin_wxallinpay') continue;
            //如果是新版app，并且app_id是‘微信支付SDK’，过滤掉。by linsuchuan 2018年3月15日 11:14:52
            if ($appVersion && $value['app_id']=='wxpaysdk') {
                continue;
            }
            if (!$appVersion && $value['app_id']=='wxallinpaysdk') {
                continue;
            }
            $payments_ret[] = $value;
    	}
    	return $payments_ret;
    }

    public function total(){

    	switch ($_REQUEST['isfastbuy']) {
    		case 'true':
    			$isfastbuy = true;
    		break;

    		case 'group':
    			$isfastbuy = 'group';
    		break;

    		default:
    			$isfastbuy = false;
    		break;
    	}

    	$this->_common(false,$isfastbuy);
    	$obj_total = new b2c_order_total();
    	$sdf_order = $_POST;
    	if($_POST){
    		$payment = json_decode($_POST['payment']['pay_app_id'],true);
    		$shipping = json_decode($_POST['shipping'],true);
    		$address = json_decode($_POST['address'],true);
    		$address_area = explode(':',$address['area']);
    	}


    	$sdf_order['cur'] = $_POST['payment']['currency'];
    	$sdf_order['shipping_id'] = $shipping['id'];
    	$sdf_order['is_protect'] = $_POST['is_protect'];
    	$sdf_order['is_tax'] = $_POST['payment']['is_tax'];
    	$sdf_order['tax_type'] = $_POST['payment']['tax_type'];
    	$sdf_order['payment'] = $payment['pay_app_id'];
    	$member_id = kernel::single('b2c_user_object')->get_member_id();
    	$sdf_order['member_id'] = $member_id;
    	$sdf_order['area_id'] = $address_area[2]?$address_area[2]:$address['area'];
    	$sdf_order['dis_point'] = floor($_POST['point']['score']);
    	$arr_cart_object = $this->pagedata['aCart'];
    	//$this->set_header();

    	$payment_detail = $obj_total->payment_detail($this,$arr_cart_object,$sdf_order);
    	return $payment_detail;
    }


    private function _common($flag=0,$is_fastbuy=false) {
    	// 购物车数据信息
    	//$aData = $this->_request->get_params(true);
        //注释了，这种写法取不到，促销信息 qianjin 2016-5-27
    	// $aData = array();
    	// if($is_fastbuy){
    	// 	$aData[0] = $is_fastbuy;
    	// 	$aData['is_fastbuy'] = $is_fastbuy;
    	// }

        //参考的商派代码 qianjin 2016-5-27
        $aData = $this->_request->get_params(true);
        if (!is_array($aData)){
            $aData = array();
        }
        $aData[0] = 'coupon';
        $aData['is_fastbuy'] = $is_fastbuy;
        $aData['apply_platform'] = '1';

    	$aCart = $this->mCart->get_objects($aData);

    	$this->_item_to_disabled( $aCart,$flag ); //处理购物扯删除项
    	$this->pagedata['aCart'] = $aCart;
        //unset($this->pagedata['aCart']['object']['goods'][2]);
        //去掉索引
        $cart_new=array();
        foreach($this->pagedata['aCart']['object']['goods'] as &$v){
            $cart_new[]=$v;
        }
        $this->pagedata['aCart']['object']['goods']=$cart_new;
    	if( $this->show_gotocart_button ) $this->pagedata['show_gotocart_button'] = 'true';

    	if( $this->ajax_update === true ) {
    		foreach(kernel::servicelist('b2c_cart_object_apps') as $object) {
    			if( !is_object($object) ) continue;

    			//应该判断是否实现了接口
    			if( !method_exists( $object,'get_update_num' ) ) continue;
    			if( !method_exists( $object,'get_type' ) ) continue;

    			$this->pagedata['edit_ajax_data'] = $object->get_update_num( $aCart['object'][$object->get_type()],$this->update_obj_ident );
    			if( $this->pagedata['edit_ajax_data'] ) {
    				//$this->pagedata['edit_ajax_data'] = json_encode( $this->pagedata['edit_ajax_data'] );
    				if( $object->get_type()=='goods' ) {
    					$this->pagedata['update_cart_type_godos'] = true;
    					if( !method_exists( $object,'get_error_html' ) ) continue;
    					$this->pagedata['error_msg'] = $object->get_error_html( $aCart['object']['goods'],$this->update_obj_ident );
    				}
    				break;
    			}
    		}
    	}


    	// 购物车是否为空
    	$this->pagedata['is_empty'] = $this->mCart->is_empty($aCart);
    	//ajax_html 删除单个商品是触发
    	if($this->ajax_html && $this->mCart->is_empty($aCart)) {
    		$arr_json_data = array(
    				'is_empty' => 'true',
    				'number'=>array(
    						'cart_number'=>$this->pagedata['aCart']['_cookie']['CART_NUMBER'],
    						'cart_count'=>$this->pagedata['aCart']['_cookie']['CART_COUNT'],
    				),
    		);
    		$this->pagedata = $arr_json_data;
    		$this->page('wap/cart/cart_empty.html', true);
    		return ;
    	}

    	// 购物车数据项的render
    	$this->pagedata['item_section'] = $this->mCart->get_item_render();

    	// 购物车数据项的render
    	$this->pagedata['item_goods_section'] = $this->mCart->get_item_goods_render();

    	// 优惠信息项render
    	$this->pagedata['solution_section'] = $this->mCart->get_solution_render();

    	//未享受的订单规则
    	$this->pagedata['unuse_rule'] = $this->mCart->get_unuse_solution_cart($aCart);

    	$imageDefault = app::get('image')->getConf('image.set');
    	$this->pagedata['defaultImage'] = $imageDefault['S']['default_image'];
    }

    private function _item_to_disabled( &$aCart,$flag ) {

    	foreach( kernel::servicelist('b2c_cart_object_apps') as $object ) {
    		if( !is_object($object) ) continue;
    		$o[$object->get_type()] = $object;
    	}

    	$arr_cart_disabled_session = $_SESSION['cart_objects_disabled_item'];
    	foreach( (array)$aCart['object'] as $_obj_type => $_arr_by_obj_type ) {
    		$tmp = $arr_cart_disabled_session[$_obj_type];
    		if( isset($arr_cart_disabled_session[$_obj_type]) ) {
    			if( !$o[$_obj_type] ) continue;
    			if( !method_exists( $o[$_obj_type],'apply_to_disabled' ) ) continue;
    			$aCart['object'][$_obj_type] = $o[$_obj_type]->apply_to_disabled( $_arr_by_obj_type, $tmp, $flag );
    			$_SESSION['cart_objects_disabled_item'][$_obj_type] = $tmp;
    		} else {
    			if( $flag )
    				unset($_SESSION['cart_objects_disabled_item'][$obj_type]);
    		}
    	}
    }

    public function get_invoice() {
        $trigger_tax = $this->app->getConf('site.trigger_tax');
        if ($trigger_tax) {
            $personal_tax_ratio = $this->app->getConf('site.personal_tax_ratio'); //个人发票税率
            $company_tax_ratio = $this->app->getConf('site.company_tax_ratio'); //公司发票税率
            $tax_content = $this->app->getConf('site.tax_content'); //发票内容选项
            if($tax_content){
                $arr_tax_content = explode(',',$tax_content);
                foreach($arr_tax_content as $tax_content_value){
                    $select_tax_content[$tax_content_value] = $tax_content_value;
                }
            }
            $tax_setting = array(
                'trigger_tax' => $trigger_tax,
                // 'personal_tax_ratio' => $personal_tax_ratio,
                // 'company_tax_ratio' => $company_tax_ratio,
                'tax_content' =>$select_tax_content ? $select_tax_content : 0,
            );

            $tax_setting['tax_list'] = array(
                0=>array(
                    'label' => '不需要发票',
                    'value' => 'false',
                ),
                1=>array(
                    'label' => $tax_setting['personal_tax_ratio'] ? '个人发票(税率'.($tax_setting['personal_tax_ratio']*100).'%)' : '',
                    'value' => 'personal',
                    'tax_ratio' => $personal_tax_ratio,
                ),
                2=>array(
                    'label' => $tax_setting['company_tax_ratio'] ? '公司发票(税率'.($tax_setting['company_tax_ratio']*100).'%)' : '',
                    'value' => 'company',
                    'tax_ratio' => $company_tax_ratio,
                ),
            );
            return $tax_setting;
        }
    }

    // 门店列表 (根据地区3级id查询门店)
    public function get_stores_list($params, $rpcService) {
        /*
        $addr_id = $params['addr_id'];
        if (!$addr_id > 0)
            $rpcService->send_user_error('error', '没有收货地址，请填写收货地址！');
        $objMem = $this->app->model('members');
        $def_addr = $objMem->getAddrById($addr_id);
        $area = explode(':',$def_addr['area']);
        $da_id = app::get('ectools')->model('regions')->getDlAreaById($area[2]);
        $list = app::get('ome')->model('branch')->getList('branch_id, name', array('region_id'=>$da_id['p_region_id']));
        */
        $list = app::get('ome')->model('branch')->getList('branch_id, name');
        return $list;
    }

}

?>
